package com.fujimana.rockincat.restree.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

public class HttpUtils {
	
	public static String negotiationContent(String accept, List<String> supported) {
		if(accept == null || accept.trim().equals("")) {
			return supported.get(0); 
		}
		
		List<String> request = splitValue(accept);
		
		Collections.sort(request, new Comparator<String>() {

			@Override
			public int compare(String o1, String o2) {
				
				int q = compareQValue(getQValue(o1),getQValue(o2));
				if(q != 0) {
					return q;
				}
				
				return comapreMediaType(getMediaType(o1), getMediaType(o2));
			}
			
			private int compareQValue(int q1, int q2) {
				if(q1 > q2) {
					return -1;
				} else if(q1 < q2) {
					return 1;
				} else {
					return 0;
				}
			}
			
			private int comapreMediaType(String c1, String c2) {
				
				int p1 = getPriority(c1);
				int p2 = getPriority(c2);
				
				if(p1 > p2) {
					return -1;
				} else if(p1 < p2) {
					return 1;
				}
				
				return 0;
			}
			
			private int getPriority(String type) {
				
				if(type.equals("*/*")) {
					return 1;
				}
				
				if(type.endsWith("/*")) {
					return 2;
				}
				
				return 3;
			}
			
			private String getMediaType(String str) {
				int index = str.indexOf(';');
				
				if(index == -1) {
					return str;
				}
				
				return str.substring(0, index);
			}
			
			private int getQValue(String str) {
				
				for(String s : str.split(";")) {
					if(s.startsWith("q=")) {
						return (int)((1-Double.parseDouble(s.substring(2))) * 10);
					}
				}
				
				return 10;
			}
		}
		
		
		);
		
		for(String str : request) {
			
			if(isMatch(str, supported)) {
				return str;
			}
			
		}
		
		return supported.get(0);
	}
	
	public static Locale negotiationLocation(String accept, List<Locale> supported) {
		// TODO
		return supported.get(0);
	}
	
	public static String negotiationCharaset(String accept, List<String> supported) {
		// TODO
		return supported.get(0);
	}
	
	private static boolean isMatch(String accept, List<String> supported) {
		return false;
	}
	
	private static List<String> splitValue(String accept) {
		List<String> list = new ArrayList<String>();
		
		for(String str : accept.split(",")) {
			list.add(str.replaceAll("\\s", ""));
		}
		
		return list;
	}

	public class HttpHeader {

		public static final String DATE = "date";

		public static final String PRAGMA = "pragma";

		public static final String CACHE_CONTROL = "cache-control";

		public static final String CONNECTION = "connection";

		public static final String TRANSFER_ENCODING = "transfer-encoding";

		public static final String UPGRADE = "upgrade";

		public static final String VIA = "Via";

		public static final String MINE_VERSION = "mime-version";

		public static final String AUTHRIZATION = "authorization";

		public static final String IF_MODIFIED_SINCE = "if-modified-since";

		public static final String REFERER = "referer";

		public static final String USER_AGENT = "user-agent";

		public static final String ACCEPT = "accept";

		public static final String ACCEPT_CHARSET = "accept-charset";

		public static final String ACCEPT_ENCODING = "accept-encoding";

		public static final String ACCEPT_LANGUAGE = "accept-language";

		public static final String HOST = "host";

		public static final String IF_MATCH = "if-match";

		public static final String IF_NONE_MATCH = "if-none-match";

		public static final String IF_RANGE = "if-range";

		public static final String IF_UNMODIFIED_SINCE = "if-unmodified-since";

		public static final String MAX_FORWARDS = "max-forwards";

		public static final String PROXY_AUTHORIZATION = "proxy-authorization";

		public static final String RANGE = "range";

		public static final String LOCATION = "location";

		public static final String SERVER = "server";

		public static final String WWW_AUTHENTICATE = "www-authenticate";

		public static final String ACCEPT_RANGES = "accept-Ranges";

		public static final String AGE = "age";

		public static final String PROXY_AUTHENTICATE = "proxy-authenticate";

		public static final String PUBLIC = "public";

		public static final String RETRY_AFTER = "retry-after";

		public static final String WARNING = "warning";

		public static final String ALTERNATES = "alternates";

		public static final String ALLOW = "allow";
		
		public static final String CONTENT_ENCODING = "content-encoding";

		public static final String CONTENT_LENGTH = "content-length";

		public static final String CONTENT_TYPE = "content-type";

		public static final String EXPIRES = "expires";

		public static final String LAST_MODIFIED = "last-modified";

		public static final String CONTENT_BASE = "content-base";

		public static final String CONTENT_LANGUAGE = "content-language";

		public static final String CONTENT_LOCATION = "content-location";

		public static final String CONTENT_MD5 = "content-md5";

		public static final String CONTENT_RANGE = "content-range";

		public static final String ETAG = "etag";
		
		public static final String CONTENT_VERSION = "content-version";

		public static final String DERIVED_FROM = "derived-from";

		public static final String LINK = "link";

		public static final String URI = "uri";

	}

	public class HttpStatusCode {

		public static final int CONTINUE = 100;

		public static final int SWITCHING_PROTOCOLS = 101;

		public static final int OK = 200;

		public static final int CREATED = 201;

		public static final int ACCEPTED = 202;

		public static final int NON_AUTTHORITATIVE_INFORMATION = 203;

		public static final int NO_CONTENT = 204;

		public static final int RESET_CONTENT = 205;

		public static final int PARTIAL_CONTENT = 206;

		public static final int MULTIPLE_CHOICES = 300;

		public static final int MOVED_PERMANENTLY = 301;

		public static final int FOUND = 302;

		public static final int SEE_OTHE = 303;

		public static final int NOT_MODIFIED = 304;

		public static final int USE_PROXY = 305;

		public static final int UNUSED = 306;

		public static final int TEMPORARY_REDIRECT = 307;

		public static final int BAD_REQUEST = 400;

		public static final int UNAUTHORIZED = 401;

		public static final int PAYMENT_REQUIRED = 402;

		public static final int FORBIDDEN = 403;

		public static final int NOT_FOUND = 404;

		public static final int METHOD_NOT_ALLOWED = 405;

		public static final int NOT_ACCEPTABLE = 406;

		public static final int PROXY_AUTHENTICATION_REQUIRED = 407;

		public static final int REQUEST_TIMEOUT = 408;

		public static final int CONFLICT = 409;

		public static final int CONE = 410;

		public static final int LENGTH_REQUIRED = 411;

		public static final int PRECONDITION_FAILED = 412;

		public static final int REQUEST_ENTITY_TOO_LARGE = 413;

		public static final int REQUEST_URI_TOO_LONG = 414;

		public static final int UNSUPPORTED_MEDIA_TYPE = 415;

		public static final int REQUEST_RANGE_NOT_SATISFIABLE = 416;

		public static final int EXCEPTION_FAILED = 417;

		public static final int INTERNAL_SERVER_ERROR = 500;

		public static final int NOT_IMPLEMENTED = 501;

		public static final int BAD_GATEWAY = 502;

		public static final int SERVICE_UNAVAILABLE = 503;

		public static final int GATEWAY_TIMEOUT = 504;

		public static final int HTTP_VERSION_NOT_SUPPORTED = 505;

	}
}
